package com.lcj.aoch.rule.controller;

import com.lcj.aoch.rule.config.ConstantConfiguration;
import com.lcj.aoch.rule.controller.form.CalculateOrderChargeForm;
import com.lcj.aoch.rule.controller.form.EstimateOrderChargeForm;
import com.lcj.aoch.rule.controller.form.SearchChargeRuleByIdForm;
import com.lcj.aoch.rule.service.ChargeRuleService;
import com.lcj.common.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping({"/charge"})
@Tag(name = "ChargeRuleController", description = "行程费用的Web接口")
public class ChargeRuleController {
  
  @Resource
  private ChargeRuleService chargeRuleService;

  @Resource
  private ConstantConfiguration constantConfiguration;


  @PostMapping({"/searchChargeRuleById"})
  @Operation(summary = "根据ID查询费用规则")
  public Result searchChargeRuleById(@RequestBody @Valid SearchChargeRuleByIdForm searchChargeRuleByIdForm) {
    HashMap res = this.chargeRuleService.searchChargeRuleById(searchChargeRuleByIdForm.getRuleId().longValue());
    return Result.ok().put("result", res);
  }
  
  @PostMapping({"/calculateOrderCharge"})
  @Operation(summary = "计算行程费用")
  public Result calculateOrderCharge(@RequestBody @Valid CalculateOrderChargeForm calculateOrderChargeForm) {
    HashMap res = this.chargeRuleService.calculateOrderCharge(
            calculateOrderChargeForm.getMileage(),
            calculateOrderChargeForm.getTime(),
            calculateOrderChargeForm.getMinute().intValue(),
            constantConfiguration.aesKey);
    return Result.ok().put("result", res);
  }
  
  @PostMapping({"/estimateOrderCharge"})
  public Result estimateOrderCharge(@RequestBody @Valid EstimateOrderChargeForm estimateOrderChargeForm) {
    HashMap res = this.chargeRuleService.calculateOrderCharge(
            estimateOrderChargeForm.getMileage(),
            estimateOrderChargeForm.getTime(),
            0,
            constantConfiguration.aesKey);
    return Result.ok().put("result", res);
  }
}
